﻿using System.Windows;
using System.Windows.Controls;
using Caliburn.Micro;

namespace MediaOwl.Common
{
    public class ViewHelper
    {

        public static void ShowAnimation(ActionExecutionContext context, string controlName, string animationName)
        {
            var control = FindControl<Control>(context, controlName);
            VisualStateManager.GoToState(control, animationName, true);
        }

        public enum ScrollTo
        {
            Top, Bottom, Left, Right
        }
        
        public enum ScrollToOffset
        {
            Vertical, Horizontal
        }

        public static void SetScroll(ActionExecutionContext context, string controlName, ScrollTo scrollTo = ScrollTo.Top)
        {
            var listBox = FindControl<ListBox>(context, controlName);
            var scrollViewer = listBox != null ? listBox.GetScrollHost() : FindControl<ScrollViewer>(context, controlName);
            
            if (scrollViewer == null)
                return;

            switch (scrollTo)
            {
                case ScrollTo.Top:
                    scrollViewer.ScrollToTop();
                    break;
                case ScrollTo.Bottom:
                    scrollViewer.ScrollToBottom();
                    break;
                case ScrollTo.Left:
                    scrollViewer.ScrollToLeft();
                    break;
                case ScrollTo.Right:
                    scrollViewer.ScrollToRight();
                    break;
            }
        }

        public static void SetScroll(ActionExecutionContext context, string controlName, double offset, ScrollToOffset scrollTo = ScrollToOffset.Vertical )
        {
            var listBox = FindControl<ListBox>(context, controlName);
            var scrollViewer = listBox != null ? listBox.GetScrollHost() : FindControl<ScrollViewer>(context, controlName);
            
            if (scrollViewer == null)
                return;

            if (scrollTo == ScrollToOffset.Vertical)
                scrollViewer.ScrollToVerticalOffset(offset);
            else
                scrollViewer.ScrollToHorizontalOffset(offset);
        }

        private static T FindControl<T>(ActionExecutionContext context, string controlName)
            where T : class
        {
            return FindControl<T>(GetFrameworkElementFromContext(context), controlName);
        }

        private static FrameworkElement GetFrameworkElementFromContext(ActionExecutionContext context)
        {
            if (context == null || context.View == null)
                return null;

            return context.View as FrameworkElement;
        }

        private static T FindControl<T>(FrameworkElement view, string controlName)
            where T : class
        {
            if (view == null || string.IsNullOrWhiteSpace(controlName))
                return null;

            return view.FindName(controlName) as T;
        } 
    }
}